package chainSimplification;

import java.io.BufferedReader;
import java.io.*;
import java.util.Random;

public class ChainSimplificationDemo {
	Plane plane;
	Path path;
	Path comparePath;

	public ChainSimplificationDemo() {
		setPlane(new Plane());
		setPath(new Path());
	}

	public void setPath(Path path) {
		this.path = path;
	}

	public void setPlane(Plane plane) {
		this.plane = plane;
	}

	public void setComparePath(Path comparePath) {
		this.comparePath = comparePath;
		comparePath.color = "red";
	}

	public void removePlane(Plane plane) {
		plane = null;
	}

	public void removePath(Path path) {
		path = null;
	}

	static Random random = new Random(5);

	public void generatePath(int length, int bound) {
		Path path = new Path();
		for (int i = 0; i < length; i++) {
			path.addVertex(new Vertex(random.nextFloat() * bound - bound / 2,
					random.nextFloat() * bound - bound / 2, random.nextFloat()
							* bound - bound / 2));
		}
		setPath(path);
		plane.followPath(path);
	}

	public void update() {
		plane.update(0.1f);
		// System.out.println(plane.coordinate);
	}

	public void replay() {
		plane.followPath(path);
	}

	// public void simplify() {
	// Path newPath = Simplification.simplifyPath(path, (float) Math.PI / 2,
	// 10f);
	// plane.followPath(path);
	// }

	public static void main(String s[]) throws Exception {
		ChainSimplificationDemo chainSimplificationDemo = new ChainSimplificationDemo();
		// chainSimplificationDemo.generatePath(5, 100);
		float angleConstraint, epsilon;
		{
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					"data.txt"));
			String line = null;
			{
				line = bufferedReader.readLine();
				String arg[] = line.split(" ");
				angleConstraint = new Float(arg[0]);
				epsilon = new Float(arg[1]);
			}
			{
				Path path = new Path();
				while ((line = bufferedReader.readLine()) != null) {
					float x, y, z;
					String arg[] = line.split(" ");
					x = new Float(arg[0]);
					y = new Float(arg[1]);
					z = new Float(arg[2]);

					path.addVertex(new Vertex(x, y, z));
				}
				chainSimplificationDemo.setComparePath(path);
				Path simPath = Simplification.simplifyPath(path,
						angleConstraint, epsilon);

				System.out.println(simPath.vertices);
				chainSimplificationDemo.setPath(simPath);
				chainSimplificationDemo.plane.followPath(simPath);
			}
		}
		while (true) {
			chainSimplificationDemo.update();
			Thread.sleep(10);
		}
	}
}
